package com.kingzoon.seckill.service.impl.account;

import java.util.HashMap;
import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ch.qos.logback.classic.Logger;
import payment.tools.util.GUID;
import payment.tools.util.TimeUtil;

import com.kingzoon.seckill.dao.UaLoginMethodMapper;
import com.kingzoon.seckill.dao.UaMemberInfoMapper;
import com.kingzoon.seckill.dto.Result;
import com.kingzoon.seckill.entity.UaLoginMethod;
import com.kingzoon.seckill.entity.UaLoginMethodExample;
import com.kingzoon.seckill.entity.UaMemberInfo;
import com.kingzoon.seckill.entity.UaMemberInfoExample;
import com.kingzoon.seckill.enums.MacroDefine;
import com.kingzoon.seckill.service.MemberRegisterService;
import com.kingzoon.seckill.service.impl.base.PasswordHelper;
import com.kingzoon.seckill.util.UtilTools;

/**
 * 手机号快速注册
 * @author raymond
 *
 */
@Service
public class MemberRegisterServiceImpl implements MemberRegisterService {
	
	private Logger log = (Logger) LoggerFactory.getLogger(MemberRegisterServiceImpl.class);
	
	private HashMap<String,Object> paramMap = new HashMap<String,Object>();
	
	@Autowired
	private UaMemberInfoMapper uaMemberInfoDao;
	
	@Autowired
	private UaLoginMethodMapper uaLoginMethodDao;
	
	@Autowired
	private PasswordHelper passwordHelper;
	
	private UaMemberInfo uaMemberInfo = new UaMemberInfo();
	private UaLoginMethod uaLoginMethod = new UaLoginMethod();
	private UaMemberInfoExample uaMemberInfoExample = new UaMemberInfoExample();
	private UaLoginMethodExample uaLoginMethodExample = new UaLoginMethodExample();

	public Result<HashMap<String, Object>> doBusiness(HashMap<String, Object> mapParam, Result<HashMap<String, Object>> rs) {
		paramMap = mapParam;
		if(preCheck(rs)){
			if(cache(rs)){
				if(check(rs)){
					if(before(rs)){
						if(clear(rs)){
							after(rs);
						}
					}
				}
			}
		}
		return rs;
	}

	public boolean preCheck(Result<HashMap<String, Object>> rs) {
		String[] keys = {"exchangeId","distributorId","phoneNo","smsCode","password"};
		String emptyString = UtilTools.isParamEmpty(keys, paramMap);
		if(!UtilTools.isEmpty(emptyString)){
			rs.setFlag(false);
			rs.setCode(MacroDefine.GS_CODE.E_1002.getCode());
			rs.setMessage(MacroDefine.GS_CODE.E_1002.getMessage());
			return false;
		}
		return true;
	}

	public boolean cache(Result<HashMap<String, Object>> rs) {
		return true;
	}


	public boolean check(Result<HashMap<String, Object>> rs) {
		//检查注册会员表
		uaMemberInfoExample.createCriteria().andMemberMobileEqualTo(UtilTools.valueOf(paramMap.get("phoneNo")));
		List<UaMemberInfo> memberList = uaMemberInfoDao.selectByExample(uaMemberInfoExample);
		if(memberList != null && memberList.size() > 0){
			rs.setFlag(false);
			rs.setCode(MacroDefine.YW_CODE.E_5150.getCode());
			rs.setMessage(MacroDefine.YW_CODE.E_5150.getMessage());
			return false;
		}
		//检查登陆方法表
		uaLoginMethodExample.createCriteria().andLoginNameEqualTo(UtilTools.valueOf(paramMap.get("phoneNo")));
		List<UaLoginMethod> methodList = uaLoginMethodDao.selectByExample(uaLoginMethodExample);
		if(methodList != null && methodList.size() > 0){
			rs.setFlag(false);
			rs.setCode(MacroDefine.YW_CODE.E_5150.getCode());
			rs.setMessage(MacroDefine.YW_CODE.E_5150.getMessage());
			return false;
		}
		//TODO 手机验证码校验
		return true;
	}

	public boolean before(Result<HashMap<String, Object>> rs) {
		//生成会员编号
		String memberId = GUID.generateGUID();
		paramMap.put("memberId", memberId);
		paramMap.put("loginName", paramMap.get("phoneNo"));
		paramMap.put("loginPwd", passwordHelper.encryptPassword((String)paramMap.get("password")));
		paramMap.put("loginType", UtilTools.isEmpty(UtilTools.valueOf(paramMap.get("loginType"))) ? "0" : paramMap.get("loginType"));
		paramMap.put("memberMobile", paramMap.get("phoneNo"));
		paramMap.put("registerDate", TimeUtil.getTimeStamp().substring(0, 8));
		paramMap.put("registerTime", TimeUtil.getTimeStamp().substring(8));
		paramMap.put("registerFromWay", UtilTools.isEmpty(UtilTools.valueOf(paramMap.get("fromWay"))) ? "0" :paramMap.get("fromWay"));
		paramMap.put("memberType", UtilTools.isEmpty(UtilTools.valueOf(paramMap.get("memberType"))) ? "0" :paramMap.get("memberType"));
		paramMap.put("insertDate", TimeUtil.getTimeStamp().substring(0, 8));
		paramMap.put("insertTime", TimeUtil.getTimeStamp().substring(8));
		paramMap.put("lockStatus", "0");
		paramMap.put("isPlatform", UtilTools.isEmpty(UtilTools.valueOf(paramMap.get("isPlatform"))) ? "0" :paramMap.get("isPlatform"));
		
		//会员信息表
		uaMemberInfo.setExchangeId(UtilTools.valueOf(paramMap.get("exchangeId")));
		uaMemberInfo.setDistributorId(UtilTools.valueOf(paramMap.get("distributorId")));
		uaMemberInfo.setMemberId(UtilTools.valueOf(paramMap.get("memberId")));
		uaMemberInfo.setLoginName(UtilTools.valueOf(paramMap.get("loginName")));
		uaMemberInfo.setLoginPwd(UtilTools.valueOf(paramMap.get("loginPwd")));
		uaMemberInfo.setMemberMobile(UtilTools.valueOf(paramMap.get("memberMobile")));
		uaMemberInfo.setRegisterDate(UtilTools.valueOf(paramMap.get("registerDate")));
		uaMemberInfo.setRegisterTime(UtilTools.valueOf(paramMap.get("registerTime")));
		uaMemberInfo.setRegisterFromWay(Long.valueOf(UtilTools.valueOf(paramMap.get("registerFromWay"))));
		uaMemberInfo.setMemberType(UtilTools.valueOf(paramMap.get("memberType")));
		uaMemberInfo.setInsertDate(UtilTools.valueOf(paramMap.get("insertDate")));
		uaMemberInfo.setInsertTime(UtilTools.valueOf(paramMap.get("insertTime")));
		uaMemberInfo.setLockStatus(UtilTools.valueOf(paramMap.get("lockStatus")));
		uaMemberInfo.setIsPlatform(Long.valueOf(UtilTools.valueOf(paramMap.get("isPlatform"))));
		
		//登陆信息表
		uaLoginMethod.setExchangeId(UtilTools.valueOf(paramMap.get("exchangeId")));
		uaLoginMethod.setDistributorId(UtilTools.valueOf(paramMap.get("distributorId")));
		uaLoginMethod.setMemberId(UtilTools.valueOf(paramMap.get("memberId")));
		uaLoginMethod.setLoginName(UtilTools.valueOf(paramMap.get("loginName")));
		uaLoginMethod.setLoginType(UtilTools.valueOf(paramMap.get("loginType")));
		uaLoginMethod.setLoginPwd(uaMemberInfo.getLoginPwd());
		return true;
	}

	@Transactional
	public boolean clear(Result<HashMap<String, Object>> rs) {
		//写入会员信息表
		int a = uaMemberInfoDao.insertSelective(uaMemberInfo);
		if(a != 1){
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}
		//写入登陆信息表
		int b = uaLoginMethodDao.insertSelective(uaLoginMethod);
		if(b != 1){
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}
		
		return true;
	}

	public boolean after(Result<HashMap<String, Object>> rs) {
		HashMap<String, Object> respMap = new HashMap<String,Object>();
		respMap.put("exchangeId", uaMemberInfo.getExchangeId());
		respMap.put("distributorId", uaMemberInfo.getDistributorId());
		respMap.put("memberId", uaMemberInfo.getMemberId());
		respMap.put("loginName", uaMemberInfo.getLoginName());
		respMap.put("lockStatus", uaMemberInfo.getLockStatus());
		rs.setData(respMap);
		rs.setFlag(true);
		rs.setCode(MacroDefine.JC_CODE.S_0000.getCode());
		rs.setMessage(MacroDefine.JC_CODE.S_0000.getMessage());
		return true;
	}

}
